﻿Imports System.Collections.ObjectModel
Imports System.Globalization

Public Class MonthlySummary

    Property Handler As DataRowMappedPropertiesHandler = New DataRowMappedPropertiesHandler(Me, "monthly_summary", True)
    Property Row As DataRow

    Sub New()
    End Sub

    Sub New(ByVal row As DataRow)
        Me.Row = row
        Me.Handler.RefreshPropertiesFromRow()
    End Sub

    Public Sub New(ByVal id As Integer)
        Me.Handler.ReadDbRecord(id)
    End Sub

    <DbFieldName("id", True)> Property Id As Integer
    <DbFieldName("month", False)> Property Month As String
    <DbFieldName("spend", False)> Property Spend As Decimal
    <DbFieldName("income", False)> Property Income As Decimal
    <DbFieldName("owner", False)> Property Owner As String
    <DbFieldName("remark", False)> Property Remark As String
    <DbFieldName("modifyby", False)> Property ModifyBy As String
    <DbFieldName("modifydt", False)> Property ModifyDatetime As DateTime

    Public ReadOnly Property Profit As Decimal
        Get
            Return Me.Income - Me.Spend
        End Get
    End Property

    Public Shared Function FindByOwnerAndPeriod(ByVal owner As String, ByVal period As String) As MonthlySummary
        Dim result As MonthlySummary = New MonthlySummary()
        Dim sql As String = "select * from monthly_summary where owner = '" + owner + "' and month = '" + period + "'"
        Dim myTable As DataTable = MysqlDataBaseConnection.RunSelect(sql).Tables(0)
        If myTable.Rows.Count > 0 Then
            result = New MonthlySummary(myTable.Rows(0))
        End If
        Return result
    End Function

    Public Shared Function FindByOwner(ByVal owner As String) As IEnumerable(Of MonthlySummary)
        Dim result As List(Of MonthlySummary) = New List(Of MonthlySummary)
        Dim sql As String = String.Format("select * from monthly_summary where owner = '{0}' order by month desc", owner)
        Dim myTable As DataTable = MysqlDataBaseConnection.RunSelect(sql).Tables(0)
        For Each row As DataRow In myTable.Rows
            result.Add(New MonthlySummary(row))
        Next
        Return result
    End Function

    Public Shared Function IsExistSummaryRecordThisMonth() As Boolean
        Dim sql As String = String.Format("select * from monthly_summary where month = '{0}'", Now.ToString("yyyy-MM"))
        Return MysqlDataBaseConnection.RunSelect(sql).Tables(0).Rows.Count > 0
    End Function

    Public Sub Create()
        Me.Handler.InsertDbRecord()
    End Sub

    Public Sub Update()
        Me.Handler.UpdateDbRecord()
    End Sub

    Public Sub Delete()
        Me.Handler.DeleteDbRecord()
    End Sub

    Public Sub GenerateMonthlySummary()

        '  	int year = Integer.parseInt(this.month.split("-")[0]);
        'int month = Integer.parseInt(this.month.split("-")[1]);
        'String date_from = this.month + "-01";

        'Calendar calendar = Calendar.getInstance(); 
        'if (month == 11){
        '	year += 1;
        '	month = 0;
        '}
        '   calendar.set(Calendar.DAY_OF_MONTH, 1); 
        '   calendar.set(Calendar.MONTH, month-1);
        '   calendar.set(Calendar.YEAR, year);
        '   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        'String date_to = formatter.format(calendar);

        'this.amout_spend = 0;
        'this.amout_income = 0;

        '  	if (this.owner.equals("IA")){
        '  		// BaseRecord
        '  		Collection<BaseRecord> BaseRecords = BaseRecord.getMonthlyRecord(date_from, date_to);
        '  		for(BaseRecord el: BaseRecords){
        '  			if(el.type.equalsIgnoreCase("垫付")){
        '  				this.amout_spend += Float.valueOf(el.amount);
        '  			}else if(el.type.equalsIgnoreCase("存入")){
        '  				this.amout_income += Float.valueOf(el.amount);
        '  			}
        '      	} 
        '  	}else{
        '  		// PersonRecord
        '  		Collection<PersonRecord> personRecords = PersonRecord.getMonthlyRecord(date_from, date_to, this.owner);
        '      	for(PersonRecord el: personRecords){
        '      		this.amout_spend += Float.valueOf(el.amount);
        '      	} 
        '  	}
        'this.amout_profit = this.amout_income - this.amout_spend;

        'this.update();

    End Sub

End Class
